package com.xxq.semester.controller.chat;


import com.xxq.semester.entity.chat.ChatMessages;
import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.service.impl.chat.ChatMessagesServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/chat-messages")
@Tag(name="聊天记录管理",description = "增删查")
public class ChatMessagesController {
    private final ChatMessagesServiceImpl chatMessagesService;
    @GetMapping("/{chatId}")
    @Operation(summary = "查询聊天记录", description = "获取该会话下所有聊天记录")
    public ResponseEntity<List<ChatMessages>> getChatMessagesByChatId(@PathVariable Integer chatId){
        List<ChatMessages> chatMessages=chatMessagesService.getByChatId(chatId);
        if(chatMessages==null || chatMessages.isEmpty()){
            return ResponseEntity.error("查询失败");
        }
        return ResponseEntity.success(" 查询成功",chatMessages);
    }
    @DeleteMapping("/{chatId}")
    @Operation(summary = "删除聊天记录", description = "删除该会话下所有聊天记录")
    public ResponseEntity<String> deleteChatMessagesByChatId(@PathVariable Integer chatId){
        boolean result=chatMessagesService.deleteByChatId(chatId);
        if(!result){
            return ResponseEntity.error("删除失败");
        }
        return ResponseEntity.success("删除成功");
    }
    @PostMapping("/addChatMessage")
    @Operation(summary = "添加聊天记录", description = "添加聊天记录")
    public ResponseEntity<ChatMessages> addChatMessage(@RequestBody ChatMessages chatMessages){
        chatMessages.setCreatedAt(LocalDateTime.now());
        boolean result=chatMessagesService.addChatMessage(chatMessages);
        if(!result){
            return ResponseEntity.error("添加失败");
        }
        return ResponseEntity.success(chatMessages);
    }
}
